package net.lainiao.mycost.controller;

import net.lainiao.mycost.bean.RoleInfo;
import net.lainiao.mycost.bean.UserInfo;
import net.lainiao.mycost.utils.Comm;
import net.lainiao.mycost.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2017/3/22.
 */
@Controller
@RequestMapping("/user/")
public class UserController extends Page {

    @RequestMapping("list.do")
    public String list(UserInfo userInfo, Model model, HttpServletRequest request) {
        if (userInfo == null) {
            userInfo = new UserInfo();
        }
        userInfo.setUserMark(Comm.MARK_YES);
        this.initPage(request);
        userInfo.setStart(this.getPageNo());
        userInfo.setLength(PAGE_NUM_BIG);
        model.addAttribute("list", userInfoService.list(userInfo));
        model.addAttribute("total", userInfoService.getCount(userInfo));
        return "user/user_list";
    }

    @RequestMapping("loadadd.do")
    public String loadadd(Model model) {
        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setStart(-1);
        List<RoleInfo> list = roleInfoService.list(roleInfo);
        model.addAttribute("list", list);
        return "user/user_add";
    }

    @RequestMapping("add.do")
    public String add(UserInfo userInfo, Model model, HttpServletRequest request) {
        if (userInfo == null) {
            userInfo = new UserInfo();
        }
        userInfo.setUserMark(Comm.MARK_YES);
        userInfoService.add(userInfo);
        return list(null, model, request);
    }

    @RequestMapping("check.do")
    @ResponseBody
    public String check(UserInfo userInfo) {
        UserInfo userInfo1 = userInfoService.getUser(userInfo);
        if (userInfo1 != null) {
            return "账号已经存在,请重新收入";
        } else {
            return "恭喜您，账号可以使用";
        }
    }

    @RequestMapping("loadupdate.do")
    public String loadupdate(UserInfo userInfo, Model model) {
        UserInfo userInfo1 = userInfoService.getUser(userInfo);
        model.addAttribute("userInfo", userInfo);
        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setStart(-1);
        List<RoleInfo> list = roleInfoService.list(roleInfo);
        model.addAttribute("list", list);
        return "user/user_update";
    }

    @RequestMapping("showuser.do")
    public String showuser(UserInfo userInfo, Model model) {
        UserInfo userInfo1 = userInfoService.getUser(userInfo);
        model.addAttribute("userInfo", userInfo1);
        return "user/user_show";
    }

    @RequestMapping("update.do")
    public String udpate(UserInfo userInfo,Model model,HttpServletRequest request){
        userInfoService.update(userInfo);
        return list(null,model,request);
    }

    @RequestMapping("delete.do")
    public String delete(Integer[] userId,Model model,HttpServletRequest request){
        userInfoService.detele(userId);
        return list(null,model,request);
    }
}
